package com.person.mengshuai.mytestdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.person.mengshuai.mytestdemo.entity.BMenu;
import com.person.mengshuai.mytestdemo.entity.BRoleMenu;
import com.person.mengshuai.mytestdemo.mapper.BMenuMapper;
import com.person.mengshuai.mytestdemo.mapper.BRoleMenuMapper;
import com.person.mengshuai.mytestdemo.service.BMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统管理-权限资源表  服务实现类
 * </p>
 *
 * @author ms
 * @since 2024-08-02
 */
@Service
public class BMenuImpl extends ServiceImpl<BMenuMapper, BMenu> implements BMenuService {
    @Autowired
    private BRoleMenuMapper roleMenuMapper;

    @Override
    public List<String> getPermissionNamesByRoleIds(List<Integer> roleIds) {
        List<BMenu> bMenus = getBMenusByRoleIds(roleIds);
        if (CollectionUtils.isEmpty(bMenus)) {
            return null;
        }
        return bMenus.stream().map(BMenu::getUrl).collect(Collectors.toList());
    }

    @Override
    public List<BMenu> getBMenusByRoleIds(List<Integer> roleIds) {
        LambdaQueryWrapper<BRoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(BRoleMenu::getRoleId, roleIds);
        List<BRoleMenu> bRoleMenus = roleMenuMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(bRoleMenus)) {
            return null;
        }
        List<Integer> menuIds = bRoleMenus.stream().map(BRoleMenu::getMenuId).collect(Collectors.toList());
        return baseMapper.selectBatchIds(menuIds);
    }
}
